package com.bdq.interaction.infrastructure.persistence.mysql.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdq.common.enums.InteractionTypeEnum;
import com.bdq.common.enums.YesOrNoEnum;
import com.bdq.interaction.infrastructure.persistence.mysql.pojo.InteractionComment;
import com.bdq.interaction.infrastructure.persistence.mysql.pojo.InteractionSingle;
import com.bdq.interaction.infrastructure.persistence.mysql.mapper.InteractionSingleMapper;
import com.bdq.interaction.infrastructure.persistence.mysql.service.InteractionSingleService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author malei
* @description 针对表【interaction_single(交互单一表)】的数据库操作Service实现
* @createDate 2024-05-14 15:04:13
*/
@Service
public class InteractionSingleServiceImpl extends ServiceImpl<InteractionSingleMapper, InteractionSingle>
    implements InteractionSingleService {

    @Override
    public Boolean cancelLike(Long contentId, Long userId) {
        return lambdaUpdate()
                .eq(InteractionSingle::getContentId, contentId)
                .eq(InteractionSingle::getUserId, userId)
                .eq(InteractionSingle::getType, InteractionTypeEnum.LIKE.getCode())
                .remove();
    }

    @Override
    public Boolean cancelCollect(Long contentId, Long userId) {
        return lambdaUpdate()
                .eq(InteractionSingle::getContentId, contentId)
                .eq(InteractionSingle::getUserId, userId)
                .eq(InteractionSingle::getType, InteractionTypeEnum.COLLECT.getCode())
                .remove();
    }

    @Override
    public List<InteractionSingle> getData(Long userId, Long contentId) {
        return lambdaQuery()
                .eq(InteractionSingle::getUserId, userId)
                .eq(InteractionSingle::getContentId, contentId)
                .eq(InteractionSingle::getIsDeleted, YesOrNoEnum.NO.getCode())
                .list();
    }

    @Override
    public InteractionSingle getData(Long userId, Long contentId, InteractionTypeEnum type) {
        return lambdaQuery()
                .eq(InteractionSingle::getUserId, userId)
                .eq(InteractionSingle::getContentId, contentId)
                .eq(InteractionSingle::getType, type.getCode())
                .eq(InteractionSingle::getIsDeleted, YesOrNoEnum.NO.getCode())
                .one();
    }

    @Override
    public IPage<InteractionSingle> pageSignUp(Long contentId, Integer pageNo, Integer pageSize) {
        Page<InteractionSingle> page = new Page<>();
        page.setCurrent(pageNo);
        page.setSize(pageSize);

        return lambdaQuery()
                .eq(InteractionSingle::getContentId, contentId)
                .eq(InteractionSingle::getType, InteractionTypeEnum.SIGN_UP.getCode())
                .eq(InteractionSingle::getIsDeleted, YesOrNoEnum.NO.getCode())
                .orderByAsc(InteractionSingle::getCreateTime)
                .page(page);
    }

    @Override
    public List<InteractionSingle> listData(Long userId, List<Long> contentIds) {
        return lambdaQuery()
                .eq(InteractionSingle::getUserId, userId)
                .in(InteractionSingle::getContentId, contentIds)
                .eq(InteractionSingle::getIsDeleted, YesOrNoEnum.NO.getCode())
                .list();
    }

    @Override
    public IPage<InteractionSingle> page(Long userId, Long type, Integer pageNo, Integer pageSize) {
        Page<InteractionSingle> page = new Page<>();
        page.setCurrent(pageNo);
        page.setSize(pageSize);

        return lambdaQuery()
                .eq(InteractionSingle::getUserId, userId)
                .eq(InteractionSingle::getType, type)
                .eq(InteractionSingle::getIsDeleted, YesOrNoEnum.NO.getCode())
                .orderByAsc(InteractionSingle::getCreateTime)
                .page(page);
    }
}




